﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace BingoLib
{
    public class ThreeD
    {
        public class Issue
        {
            public static DataTable getAll()
            {
                string sql = "select [pid],[RunDate],[Num] from [QT_3D_Issue] order by [pid] desc";
                DataTable dt = new QT.DBO.OleDBHelper(myDB.connstring).DataTable(sql);
                return dt;
            }

            public static DataTable getTop(int max)
            {
                string sql = "select top " + max + " [pid],[RunDate],[Num] from [QT_3D_Issue] order by [pid] desc";
                DataTable dt = new QT.DBO.OleDBHelper(myDB.connstring).DataTable(sql);
                return dt;
            }

            public static int Add(Struct3D lo, bool b)
            {
                if (IsExist(lo.pid))
                {
                    if (b) return Update(lo);
                    else return -2;
                }

                int rval = -1;
                string sql = "insert into [QT_3D_Issue]([pid],[rundate],[num]) values('" + lo.pid + "','" + lo.rundate + "','" + lo.num + "')";
                rval = new QT.DBO.OleDBHelper(myDB.connstring).ExecuteNonQuery(sql);
                if (rval <= 0) return rval;

                return Number.Add(lo);
            }

            public static int Update(Struct3D lo)
            {
                int rval = -1;

                string sql = "update [QT_3D_Issue] set [rundate]='" + lo.rundate + "',[num]='" + lo.num + "' where [pid]='" + lo.pid + "'";
                rval = new QT.DBO.OleDBHelper(myDB.connstring).ExecuteNonQuery(sql);
                if (rval <= 0) return rval;

                return Number.Add(lo);
            }

            public static int Delete(List<string> pids)
            {
                if (Number.Delete(pids) == -1) return -1;

                string sql = "delete from [QT_3D_Issue] where [pid] in('" + string.Join("','", pids.ToArray()) + "')";
                return new QT.DBO.OleDBHelper(myDB.connstring).ExecuteNonQuery(sql);
            }

            public static bool IsExist(string pid)
            {
                bool b = false;

                string sql = "select [pid] from [QT_3D_Issue] where [pid]='" + pid + "'";
                DataTable dt = new QT.DBO.OleDBHelper(myDB.connstring).DataTable(sql);
                if (dt.Rows.Count > 0) b = true;

                return b;
            }
        }

        public class Number
        {
            public static int Add(Struct3D lo)
            {
                int rval = 0;
                string sql = "";

                if (Delete(new List<string>(new string[] { lo.pid })) == -1) return -1;

                string[] nums = lo.num.Split(';');
                for (int i = 0; i < nums.Length;i++ )
                {
                    sql = "insert into [QT_3D_Number]([pid],[order],[num]) values('" + lo.pid + "'," + i + "," + nums[i] + ")";
                    if (new QT.DBO.OleDBHelper(myDB.connstring).ExecuteNonQuery(sql) == 1) rval = 1;
                }

                return rval;
            }

            public static int Delete(List<string> pids)
            {
                string sql = "delete from [QT_3D_Number] where [pid] in('" + string.Join("','", pids.ToArray()) + "')";
                return new QT.DBO.OleDBHelper(myDB.connstring).ExecuteNonQuery(sql);
            }
        }

        public class Setting
        {
            public static string get(string attrib)
            {
                string sql = "select [content] from [QT_3D_Setting] where [Attrib]='" + attrib + "'";
                DataTable dt = new QT.DBO.OleDBHelper(myDB.connstring).DataTable(sql);
                if (dt.Rows.Count > 0) return dt.Rows[0][0].ToString();
                else return "";
            }

            public static int update(string attrib, string content)
            {
                string sql = "update [QT_3D_Setting] set [content]='" + content + "' where [Attrib]='" + attrib + "'";
                return new QT.DBO.OleDBHelper(myDB.connstring).ExecuteNonQuery(sql);
            }
        }
    }

    public struct Struct3D
    {
        public string pid;
        public string rundate;
        public string num;
    }
}
